<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<!-- richtext.qdoc -->
  <title>Document Layouts | Qt GUI 5.14.2</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.14</a></td><td ><a href="qtgui-index.html">Qt GUI</a></td><td >Document Layouts</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtgui-index.html">Qt 5.14.2 Reference Documentation</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
  <link rel="prev" href="richtext-cursor.html" />
  <link rel="next" href="richtext-common-tasks.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="richtext-cursor.html">The QTextCursor Interface</a>
<span class="naviSeparator">  &#9702;  </span>
<a class="nextPage" href="richtext-common-tasks.html">Common Rich Text Editing Tasks</a>
</p><p/>
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#overview">Overview</a></li>
<li class="level1"><a href="#example-shaped-text-layout">Example - Shaped Text Layout</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Document Layouts</h1>
<span class="subtitle"></span>
<!-- $$$richtext-layouts.html-description -->
<div class="descr"> <a name="details"></a>
<p>The layout of a document is only relevant when it is to be displayed on a device, or when some information is requested that requires a visual representation of the document. Until this occurs, the document does not need to be formatted and prepared for a device.</p>
<a name="overview"></a>
<h2 id="overview">Overview</h2>
<p>Each document's layout is managed by a subclass of the <a href="qabstracttextdocumentlayout.html">QAbstractTextDocumentLayout</a> class. This class provides a common interface for layout and rendering engines. The default rendering behavior is currently implemented in a private class. This approach makes it possible to create custom layouts, and provides the mechanism used when preparing pages for printing or exporting to Portable Document Format (PDF) files.</p>
<a name="example-shaped-text-layout"></a>
<h2 id="example-shaped-text-layout">Example - Shaped Text Layout</h2>
<p>Sometimes it is important to be able to format plain text within an irregularly-shaped region, perhaps when rendering a custom widget, for example. Scribe provides generic features, such as those provided by the <a href="qtextlayout.html">QTextLayout</a> class, to help developers perform word-wrapping and layout tasks without the need to create a document first.</p>
<p class="centerAlign"><img src="images/plaintext-layout.png" alt="" /></p><p>Formatting and drawing a paragraph of plain text is straightforward. The example below will lay out a paragraph of text, using a single font, around the right hand edge of a circle.</p>
<pre class="cpp">

      <span class="type"><a href="qtextlayout.html">QTextLayout</a></span> textLayout(text<span class="operator">,</span> font);
      <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> margin <span class="operator">=</span> <span class="number">10</span>;
      <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> radius <span class="operator">=</span> <a href="../qtcore/qtglobal.html#qMin">qMin</a>(width()<span class="operator">/</span><span class="number">2.0</span><span class="operator">,</span> height()<span class="operator">/</span><span class="number">2.0</span>) <span class="operator">-</span> margin;
      <span class="type"><a href="qfontmetrics.html">QFontMetrics</a></span> fm(font);

      <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> lineHeight <span class="operator">=</span> fm<span class="operator">.</span>height();
      <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> y <span class="operator">=</span> <span class="number">0</span>;

      textLayout<span class="operator">.</span>beginLayout();

      <span class="keyword">while</span> (<span class="number">1</span>) {
          <span class="comment">// create a new line</span>
          <span class="type"><a href="qtextline.html">QTextLine</a></span> line <span class="operator">=</span> textLayout<span class="operator">.</span>createLine();
          <span class="keyword">if</span> (<span class="operator">!</span>line<span class="operator">.</span>isValid())
              <span class="keyword">break</span>;

          <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> x1 <span class="operator">=</span> <a href="../qtcore/qtglobal.html#qMax">qMax</a>(<span class="number">0.0</span><span class="operator">,</span> pow(pow(radius<span class="operator">,</span><span class="number">2</span>)<span class="operator">-</span>pow(radius<span class="operator">-</span>y<span class="operator">,</span><span class="number">2</span>)<span class="operator">,</span> <span class="number">0.5</span>));
          <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> x2 <span class="operator">=</span> <a href="../qtcore/qtglobal.html#qMax">qMax</a>(<span class="number">0.0</span><span class="operator">,</span> pow(pow(radius<span class="operator">,</span><span class="number">2</span>)<span class="operator">-</span>pow(radius<span class="operator">-</span>(y<span class="operator">+</span>lineHeight)<span class="operator">,</span><span class="number">2</span>)<span class="operator">,</span> <span class="number">0.5</span>));
          <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> x <span class="operator">=</span> <a href="../qtcore/qtglobal.html#qMax">qMax</a>(x1<span class="operator">,</span> x2) <span class="operator">+</span> margin;
          <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> lineWidth <span class="operator">=</span> (width() <span class="operator">-</span> margin) <span class="operator">-</span> x;

          line<span class="operator">.</span>setLineWidth(lineWidth);
          line<span class="operator">.</span>setPosition(<span class="type"><a href="../qtcore/qpointf.html">QPointF</a></span>(x<span class="operator">,</span> margin<span class="operator">+</span>y));
          y <span class="operator">+</span><span class="operator">=</span> line<span class="operator">.</span>height();
      }

      textLayout<span class="operator">.</span>endLayout();

      <span class="type"><a href="qpainter.html">QPainter</a></span> painter;
      painter<span class="operator">.</span>begin(<span class="keyword">this</span>);
      painter<span class="operator">.</span>setRenderHint(<span class="type"><a href="qpainter.html">QPainter</a></span><span class="operator">::</span>Antialiasing);
      painter<span class="operator">.</span>fillRect(rect()<span class="operator">,</span> <span class="type"><a href="qt-sub-qtgui.html">Qt</a></span><span class="operator">::</span>white);
      painter<span class="operator">.</span>setBrush(<span class="type"><a href="qbrush.html">QBrush</a></span>(<span class="type"><a href="qt-sub-qtgui.html">Qt</a></span><span class="operator">::</span>black));
      painter<span class="operator">.</span>setPen(<span class="type"><a href="qpen.html">QPen</a></span>(<span class="type"><a href="qt-sub-qtgui.html">Qt</a></span><span class="operator">::</span>black));
      textLayout<span class="operator">.</span>draw(<span class="operator">&amp;</span>painter<span class="operator">,</span> <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span>(<span class="number">0</span><span class="operator">,</span><span class="number">0</span>));

      painter<span class="operator">.</span>setBrush(<span class="type"><a href="qbrush.html">QBrush</a></span>(<span class="type"><a href="qcolor.html">QColor</a></span>(<span class="string">&quot;#a6ce39&quot;</span>)));
      painter<span class="operator">.</span>setPen(<span class="type"><a href="qpen.html">QPen</a></span>(<span class="type"><a href="qt-sub-qtgui.html">Qt</a></span><span class="operator">::</span>black));
      painter<span class="operator">.</span>drawEllipse(<span class="type"><a href="../qtcore/qrectf.html">QRectF</a></span>(<span class="operator">-</span>radius<span class="operator">,</span> margin<span class="operator">,</span> <span class="number">2</span><span class="operator">*</span>radius<span class="operator">,</span> <span class="number">2</span><span class="operator">*</span>radius));
      painter<span class="operator">.</span>end();

</pre>
<p>We create a text layout, specifying the text string we want to display and the font to use. We ensure that the text we supplied is formatted correctly by obtaining text lines from the text format, and wrapping the remaining text using the available space. The lines are positioned as we move down the page.</p>
<p>The formatted text can be drawn onto a paint device; in the above code, the text is drawn directly onto a widget.</p>
</div>
<!-- @@@richtext-layouts.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="richtext-cursor.html">The QTextCursor Interface</a>
<span class="naviSeparator">  &#9702;  </span>
<a class="nextPage" href="richtext-common-tasks.html">Common Rich Text Editing Tasks</a>
</p>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2020 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
